package com.yonghui;

import lombok.extern.slf4j.Slf4j;

import java.lang.ref.SoftReference;
import java.util.concurrent.TimeUnit;

/**
 * 软引用占满老年代测试
 */
@Slf4j
public class SoftReferenceGC {
    public static void main(String[] args) {
        log.info("========创建软引用=========");
        SoftReference sf=new SoftReference(new Student("张三",22));
        log.info("打印对象{}",sf.get());
        log.info("========执行gc=======");
        System.gc();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("========After gc========");
        log.info("打印对象{}",sf.get());

        try {
//            byte[] b=new byte[1024*1024*1];
            byte[] b=new byte[1024*7168-1024*1300];
            log.info("打印对象{}",sf.get());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
